#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=4009;
const ll K=209;
const ll M=30009;
ll T,n,m,val[N],tot,ans,db,tp;
int dst[N][N];
bool ok[M*2];
struct Point{ll x,y;} p[N];
bool check(ll x){
    if(x%20!=3&&x>10||x==1)
        return 0;
    for(ll i=2;i*i<=x;i++){
        if(x%i==0)
            return 0;
    }
    return 1;
}
void solve(){
    ans=db=tp=0;
    tot=0;
    scanf("%lld%lld",&n,&m);
    for(ll i=1;i<=n;i++)
        scanf("%lld%lld",&p[i].x,&p[i].y);
    for(ll i=1;i<=n;i++)
        scanf("%lld",&val[i]);
    for(ll i=1;i<=n;i++)
        for(ll j=1;j<=n;j++)
            dst[i][j]=abs(p[i].x-p[j].x)+abs(p[i].y-p[j].y);
    for(ll i=1;i<=m*2;i++)
        ok[i]=check(i);
    ll a,b,d,tmp;
    for(ll i=1;i<=n;i++){
        for(ll j=i+1;j<=n;j++){
            d=dst[i][j];
            if(!ok[d])
                continue;
            for(ll k=1;k<=n;k++){
                if(k==i||k==j)continue;
                ll a=dst[i][k];
                ll b=dst[j][k];
                if(a<=d&&d<=b||b<=d&&d<=a)
                    ans+=val[i]+val[j]+val[k];
                if(a==d||b==d)
                    db+=val[i]+val[j]+val[k];
                if(a==d&&b==d)
                    tp+=val[i]+val[j]+val[k];
            }
        }
    }
    printf("%lld\n",ans-(db+tp)/2+tp/3);
}
int main(){
    freopen("chess.in","r",stdin);
    freopen("chess.out","w",stdout);
    scanf("%lld",&T);
    while(T--)solve();
    return 0;
}